﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainBallCtr : MonoBehaviour {
    public GameObject mainBall;
    public GameObject gm;
    public GameObject shadowPrefab;
    GameObject cvs;
    public float gravityValue;
    public float accelerateValue;
    public float rotateAngles;

    private GameObject multipleText;

    public static float mainBallRadius = 40.5f;
    static float rotateBonusTime = 1.0f;

    public float mainBallSpeed;
    Vector3 gravityDirection;
    Vector3 oldPos;

    float lastPositionX = .0f;

    float rotateBonusTimer = .0f;

	// Use this for initialization
	void Start () {
        mainBallSpeed = .0f;
        oldPos = mainBall.GetComponent<RectTransform>().anchoredPosition3D;
        gravityDirection = -oldPos / Vector3.Distance(oldPos, Vector3.zero);
        cvs = gm.GetComponent<GameCtr>().cvs;

        multipleText = GameObject.FindGameObjectWithTag("MultipleTextTag");

        //StartCoroutine(Shadow());
	}
	
	// Update is called once per frame
	void LateUpdate () {
        
        if (GameCtr.isPause == false)
        {
            RectTransform rectTransformMainBall = mainBall.GetComponent<RectTransform>();
            oldPos = rectTransformMainBall.anchoredPosition3D;
            gravityDirection = -oldPos / Vector3.Distance(oldPos, Vector3.zero);
            OnButton();
            mainBallSpeed += gravityValue;
            rectTransformMainBall.anchoredPosition3D += mainBallSpeed * Time.deltaTime * gravityDirection;

            IsMultiple(this.lastPositionX, rectTransformMainBall.anchoredPosition3D);

            if (GameCtr.isRotateBonus)
            {
                rotateBonusTimer = OnRotateBonus(rotateBonusTimer);
            }

            if (Vector3.Distance(mainBall.GetComponent<RectTransform>().anchoredPosition3D, Vector3.zero) < 103.0f)
            {
                GameCtr.isPause = true;
                GameCtr.isGameOver = true;
                Destroy(mainBall);
            }
        }
    }

    void OnButton()
    {
        //Vector3 deltaPos;
        if (Input.GetKey(KeyCode.Space))
        {
            
            if (mainBallSpeed > 0)
            {
                mainBall.transform.RotateAround(cvs.transform.position, -Vector3.forward, rotateAngles * Time.deltaTime);
                mainBallSpeed += 0.1f * accelerateValue;
                //deltaPos = Quaternion.Euler(0, 0, 50) * gravityDirection;
            }
            else
            {
                mainBall.transform.RotateAround(cvs.transform.position, -Vector3.forward, rotateAngles * Time.deltaTime);
                //deltaPos = Quaternion.Euler(0, 0, 90) * gravityDirection * 2.0f;
            }

            //mainBall.GetComponent<RectTransform>().anchoredPosition3D += deltaPos * accelerateValue;

            oldPos = mainBall.GetComponent<RectTransform>().anchoredPosition3D;
            gravityDirection = -oldPos / Vector3.Distance(oldPos, Vector3.zero);
        }
    }

    public float OnRotateBonus(float timer)
    {
        Vector3 deltaPos = Vector3.zero;
        timer += Time.deltaTime;
        
        if(timer < rotateBonusTime)
        {
            RectTransform mainBallRT = mainBall.GetComponent<RectTransform>();
            deltaPos = Quaternion.Euler(0, 0, 90) * gravityDirection * Mathf.PI * Vector3.Distance(mainBallRT.anchoredPosition3D, Vector3.zero) / rotateBonusTime * Time.deltaTime;
            mainBallRT.anchoredPosition3D += deltaPos;
        }
        else
        {
            GameCtr.isRotateBonus = false;
            timer = .0f;
        }

        return timer;
    }

    private void IsMultiple(float lastPositionX, Vector3 position)
    {
        if(lastPositionX < 0 && position.x >= 0)
        {
            int multiple = ++GameCtr.GameData.multiple;
            ScoreText text = multipleText.GetComponent<ScoreText>();
            text.timer = .0f;
            text.stopFlag = false;
            text.startFlag = false;
            text.Init(2, 0, multiple);
        }
        this.lastPositionX = position.x;
    }

    // Test
    IEnumerator Shadow()
    {
        GameObject bg = gm.GetComponent<GameCtr>().bg;
        while (true)
        {
            GameObject go = Instantiate(shadowPrefab, bg.transform);
            go.GetComponent<RectTransform>().anchoredPosition3D = mainBall.GetComponent<RectTransform>().anchoredPosition3D;
            go.GetComponent<Image>().color = new Color(255, 255, 255, 1);
            go.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            Destroy(go, 0.1f);
            //yield return new WaitForSeconds(0.1f);
            yield return null;
        }
    }
}
